package cn.wolfcode.trip.app.controller;


import cn.wolfcode.trip.base.domain.GoodsCollection;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.IGoodsCollectionService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品收藏控制器
 */
@RestController
@RequestMapping("goodsCollections")
public class GoodsCollectionController {

    @Autowired
    private IGoodsCollectionService goodsCollectionService;

    @PostMapping("commodity")
    @ResponseBody
    public JSONResult save(GoodsCollection goodsCollection){
        JSONResult jsonResult = new JSONResult();
        User user = UserContext.getCurrentUser();
        goodsCollection.setUser(user);
        try{
            goodsCollectionService.save(goodsCollection);
            return jsonResult;
        }catch(Exception e){
            return jsonResult.mark(e.getMessage());
        }
    }

    @GetMapping("/{userId}")
    @ResponseBody
    public List<GoodsCollection> getByUserId(@PathVariable Long userId){
        return goodsCollectionService.getByUserId(userId);
    }
}
